<template>
  <el-container class="home_container">
    <!--头部区域 -->
    <el-header>
      <div>
        <img src="../assets/heima.png" alt="">
        <span>电商后台管理系统</span>
      </div>
      <el-button type="info" @click="logout">退出</el-button>
    </el-header>
    <!-- 主体区域 -->
    <el-container>
      <!-- 侧边栏区域 -->
      <el-aside :width="isCollapse?'64px':'200px'">
        <div class="toggle" @click="btnCollapse">|||</div>
        <el-menu background-color="#333744" text-color="#fff"
          active-text-color="#409eff" unique-opened :collapse=isCollapse
          :collapse-transition=false router :default-active=activePath>
          <!-- 一级菜单 -->
          <el-submenu :index="item.id+''" v-for="item in menuList"
            :key="item.id">
            <template slot="title">
              <i :class="iconsObj[item.id]"></i>
              <span>{{item.authName}}</span>
            </template>
            <!-- 二级菜单 -->
            <el-menu-item :index="'/'+obj.path" v-for="obj in item.children"
              :key="obj.id" @click="changeActivePath('/'+obj.path)">
              <template slot="title">
                <i class="el-icon-menu"></i>
                <span>{{obj.authName}}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 内容主体区域 -->
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data () {
    return {
      // 用户菜单数据
      menuList: [],
      iconsObj: {
        125: 'iconfont icon-user',
        103: 'iconfont icon-tijikongjian',
        101: 'iconfont icon-shangpin',
        102: 'iconfont icon-danju',
        145: 'iconfont icon-baobiao'
      },
      // 折叠菜单
      isCollapse: false,
      // 二级菜单激活路由
      activePath: ''
    }
  },
  created () {
    // 页面打开渲染一次用户菜单
    this.getMenus()
    // 打开页面激活导航
    this.activePath = window.sessionStorage.getItem('activePath')
  },
  methods: {
    // 退出按钮
    logout () {
      window.sessionStorage.clear()
      this.$router.push('/login')
      this.$message.success('退出成功')
    },
    // 渲染用户菜单列表数据
    async getMenus () {
      const { data: res } = await this.$http.get('menus')
      if (res.meta.status !== 200) return this.$message.error('获取菜单列表失败')
      // console.log(res)
      this.menuList = res.data
    },
    // 折叠按钮
    btnCollapse () {
      this.isCollapse = !this.isCollapse
    },
    // 保存用户点击菜单激活状态
    changeActivePath (activePath) {
      window.sessionStorage.setItem('activePath', activePath)
      this.activePath = activePath
    }
  }

}
</script>

<style scoped lang='less'>
.home_container {
  height: 100%;
}
// 头部
.el-header {
  background-color: #373d41;
  display: flex;
  justify-content: space-between;
  align-items: center;
  // logo
  div {
    display: flex;
    align-items: center;
    //标题
    span {
      font-size: 20px;
      color: #fff;
      margin-left: 15px;
    }
  }
}
//侧边栏
.el-aside {
  background-color: #333744;
  // 菜单
  .el-menu {
    border-right: none;
  }
  .el-submenu i {
    margin-right: 5px;
  }
  // 折叠
  .toggle {
    padding: 4px 0;
    text-align: center;
    font-size: 12px;
    color: #fff;
    background-color: #4a5064;
    cursor: pointer;
    letter-spacing: 2px;
  }
}
.el-main {
  background-color: #eaedf1;
}
</style>
